home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / players.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  5KB  |  187 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "common.h"
  5. #include "book.h"
  6.  
  7. #define PLAYERFILE "players.dat"
  8.  
  9. int totalplayers = 0;
  10.  
  11. #define MAXPLAYERS 500
  12.  
  13. typedef struct {
  14.   char player[100];
  15.   int wins;
  16.   int losses;
  17.   int draws;
  18. } playerentry;
  19.  
  20. playerentry playerdb[MAXPLAYERS];
  21. static char lname[100];
  22.  
  23. static int rscorecompare(const void *aa, const void *bb)
  24. {
  25.     const playerentry *a = (const playerentry *)aa;
  26.     const playerentry *b = (const playerentry *)bb;   
  27.     float ascore, bscore;
  28.     ascore = (a->wins+(a->draws/2))/(a->wins+a->draws+a->losses);
  29.     bscore = (b->wins+(b->draws/2))/(b->wins+b->draws+b->losses);
  30.     if (ascore > bscore) return(-1);
  31.     else if (bscore > ascore) return(1);
  32.     else return(0);
  33. }
  34.  
  35. static int scorecompare(const void *aa, const void *bb)
  36. {
  37.     const playerentry *a = (const playerentry *)aa;
  38.     const playerentry *b = (const playerentry *)bb;   
  39.     int ascore, bscore;
  40.     ascore = 100*(a->wins+(a->draws/2))/(a->wins+a->draws+a->losses);
  41.     bscore = 100*(b->wins+(b->draws/2))/(b->wins+b->draws+b->losses);
  42.     if (bscore > ascore) return(1);
  43.     else if (bscore < ascore) return(-1);
  44.     else return(0);
  45. }
  46.  
  47. static int namecompare(const void *aa, const void *bb)
  48. {
  49.     const playerentry *a = (const playerentry *)aa;
  50.     const playerentry *b = (const playerentry *)bb;   
  51.     if (strcmp(a->player,b->player) > 0) return(1);
  52.     else if (strcmp(a->player,b->player) < 0) return(-1);
  53.     else return(0);
  54. }
  55.  
  56. void DBSortPlayer (const char *style)
  57. {
  58.   if (strncmp(style,"score",5) == 0) {
  59.     qsort(&playerdb,totalplayers,sizeof(playerentry),scorecompare);
  60.   } else if (strncmp(style,"name",4) == 0) {
  61.     qsort(&playerdb,totalplayers,sizeof(playerentry),namecompare);
  62.   } else if (strncmp(style,"reverse",7) == 0) {
  63.     qsort(&playerdb,totalplayers,sizeof(playerentry),rscorecompare);
  64.   }
  65. }
  66.  
  67. void DBListPlayer (const char *style)
  68. {
  69.   int i;
  70.     
  71.   DBReadPlayer ();
  72.   DBSortPlayer (style);
  73.   for (i = 0; i < totalplayers; i++) {
  74.     printf("%s %2.0f%% %d %d %d\n",
  75.     playerdb[i].player,
  76.     100.0*(playerdb[i].wins+((float)playerdb[i].draws/2))/
  77.      (playerdb[i].wins+playerdb[i].draws+playerdb[i].losses),
  78.     playerdb[i].wins,
  79.     playerdb[i].losses,
  80.     playerdb[i].draws);
  81.     if ((i+1) % 10 == 0) {printf("[Type a character to continue.]\n"); getchar();}
  82.   }
  83. }
  84.  
  85. void DBWritePlayer (void)
  86. {
  87.    int i;
  88.    float result1;
  89.    int result2;
  90.    FILE *wfp;
  91.    DBSortPlayer ("reverse");
  92.    if ((wfp = fopen(PLAYERFILE,"w")) != NULL) {
  93.      for (i = 0; i < totalplayers; i++) {
  94.         result1 =
  95.         100.0*(playerdb[i].wins+((float)playerdb[i].draws/2))/
  96.        (playerdb[i].wins+playerdb[i].draws+playerdb[i].losses),
  97.     result2 = (int) result1;
  98.         fprintf(wfp,"%s %d %d %d\n",
  99.       playerdb[i].player,
  100.           playerdb[i].wins,
  101.           playerdb[i].losses,
  102.       playerdb[i].draws);
  103.      }
  104.    }
  105.    fclose(wfp);
  106. }
  107.  
  108. void DBReadPlayer (void)
  109. {
  110.    FILE *rfp;
  111.    int n;
  112.    totalplayers = 0;
  113.    if ((rfp = fopen(PLAYERFILE,"r")) != NULL) {
  114.     while (!feof(rfp)) {
  115.      n = fscanf(rfp,"%s %d %d %d\n",
  116.     playerdb[totalplayers].player,
  117.         &playerdb[totalplayers].wins,
  118.         &playerdb[totalplayers].losses,
  119.         &playerdb[totalplayers].draws);
  120.      if (n == 4) totalplayers++;
  121.     }
  122.     fclose(rfp);
  123.    }
  124. }
  125.  
  126. int DBSearchPlayer (const char *player)
  127. {
  128.   int index = -1;
  129.   int i;
  130.  
  131.   for (i = 0; i < totalplayers; i++)
  132.     if (strncmp(playerdb[i].player,player,strlen(playerdb[i].player)) == 0)
  133.     {
  134.       index = i;
  135.       break;
  136.     }
  137.   return (index);
  138. }
  139.  
  140. void DBUpdatePlayer (const char *player, const char *resultstr)
  141. {
  142.   const char *p;
  143.   char *x;
  144.   int index;
  145.   int result = R_NORESULT;
  146.  
  147.   memset(lname,0,sizeof(lname));
  148.   p = player;
  149.   x = lname;
  150.   strcpy(lname,player);
  151.   do {
  152.     if (*p != ' ') 
  153.       *x++ = *p++;
  154.     else
  155.     p++;
  156.   } while (*p != '\0');
  157.   *x = '\000';
  158.   memset(playerdb,0,sizeof(playerdb[MAXPLAYERS]));
  159.   DBReadPlayer ();
  160.   index = DBSearchPlayer (lname);
  161.   if (index == -1) {
  162.     strcpy(playerdb[totalplayers].player,lname);
  163.     playerdb[totalplayers].wins = 0;
  164.     playerdb[totalplayers].losses = 0;
  165.     playerdb[totalplayers].draws = 0;
  166.     index = totalplayers;
  167.     totalplayers++;
  168.   }
  169.   if (strncmp(resultstr,"1-0",3) == 0)
  170.      result = R_WHITE_WINS;
  171.   else if (strncmp(resultstr,"0-1",3) == 0)
  172.      result = R_BLACK_WINS;
  173.   else if (strncmp(resultstr,"1/2-1/2",7) == 0)
  174.      result = R_DRAW;
  175.  
  176.   if ((computerplays == white && result == R_WHITE_WINS)||
  177.       (computerplays == black && result == R_BLACK_WINS))
  178.     playerdb[index].wins++;
  179.   else if ((computerplays == white && result == R_BLACK_WINS)||
  180.       (computerplays == black && result == R_WHITE_WINS))
  181.     playerdb[index].losses++;
  182.   else
  183.     /* Shouln't one check for draw here? Broken PGN files surely exist */
  184.     playerdb[index].draws++;
  185.   DBWritePlayer ();
  186. }
  187.